﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ForkCarRelationManager.Business;
using ForkCarRelationManager.Utility;
using ForkCarRelationManager.DB;
using common;
namespace ForkCarRelationManager
{
    public partial class FormClientDetail : Form
    {
        Util util = new Util();
       public String clientId;
       public String clientName;
       ClientBI clientBI = new ClientBI();
       RepireBI repireBI = new RepireBI();
        public FormClientDetail()
        {
            InitializeComponent();
        }

        private void FormClientDetail_Load(object sender, EventArgs e)
        {
            this.initData();
        }
        //初始化数据
        private void initData()
        {
            //初始化公司列表
            DataTable companyTable = clientBI.initCompany();
            this.TBCompany.DataSource = companyTable;
            this.TBCompany.DisplayMember = "ScompanyName";
            this.TBCompany.ValueMember = "ID";

            //初始化客户资料
           DataTable clientTable = clientBI.findClientById(clientId);
           this.initClientData(clientTable);
        }

        /*
         * 初始化客户资料
         * */
        private void initClientData(DataTable clientTable)
        {
            clientName = clientTable.Rows[0]["ClientName"].ToString();
            this.TBClientName.Text = clientTable.Rows[0]["ClientName"].ToString();
            this.TBClientLocation.Text = clientTable.Rows[0]["Location"].ToString();
            this.TBPlace.Text = clientTable.Rows[0]["ClientPlace"].ToString();
            this.TBCompany.Text = clientTable.Rows[0]["ScompanyName"].ToString();
            this.TBContactName.Text = clientTable.Rows[0]["ContactName"].ToString();
            this.TBPartment.Text = clientTable.Rows[0]["ContactDiv"].ToString();
            this.TBPhone.Text = clientTable.Rows[0]["ContactPhone"].ToString();
            this.TBLoadTime.Text = clientTable.Rows[0]["LoadTime"].ToString();
            this.TBMark.Text = clientTable.Rows[0]["ClientMark"].ToString();
            this.TBContactPosition.Text = clientTable.Rows[0]["ContactPosition"].ToString();
            this.TBSalesMan.Text = clientTable.Rows[0]["SalesmanName"].ToString();
            //初始化车的列表
            DataTable carsTable = clientBI.findCarsByClientId(clientId);
            this.carDataGridView.DataSource = carsTable;
            //有问题
            //初始化维修记录（默认显示第一辆车的维修记录）
            if (carsTable.Rows.Count>0)
            {
                DataTable maintenTable = clientBI.findCarMaintenRecordByCarId(carsTable.Rows[0]["ID"].ToString());
                this.MaintenDataGridView.DataSource = maintenTable;
            }
        }

        //查看车详情
        private void carDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.carDataGridView.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.RowIndex >= 0)
            {
                string s = carDataGridView.CurrentRow.Cells[7].Value.ToString();
                FormAddCar addCar = new FormAddCar(Convert.ToInt32(s));
                addCar.ShowDialog();
                if (addCar.DialogResult == DialogResult.Yes)
                {
                    //初始化车的列表
                    DataTable carsTable = clientBI.findCarsByClientId(clientId);
                    this.carDataGridView.DataSource = carsTable;
                }
            }
        }
        /*
         * 点击车的某一行
         * */
        private void carDataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.carDataGridView.CurrentCell.ColumnIndex==1)
            {
               String carId = carDataGridView.CurrentRow.Cells[7].Value.ToString();
               DataTable maintenTable = clientBI.findCarMaintenRecordByCarId(carId);
               this.MaintenDataGridView.DataSource = maintenTable;
            }
        }

        /*
         * 店家保存按钮，修改客户资料
         * 
         * */
         
        private void buttonX1_Click(object sender, EventArgs e)
        {
            bool ifsub = true;
            if (TBClientName.Text.Trim() == "")
            {
                MessageBox.Show("客户姓名不能为空");
                ifsub = false;
            }
            if (TBCompany.Text.Trim() == "")
            {
                MessageBox.Show("客户公司不能为空");
                ifsub = false;
            }
            if (TBContactName.Text.Trim() == "")
            {
                MessageBox.Show("联系人姓名不能为空");
                ifsub = false;
            }
            if (TBPhone.Text.Trim() == "")
            {
                MessageBox.Show("联系人电话不能为空");
                ifsub = false;
            }
            if(ifsub)
            {
                if (clientBI.UpdateClient(TBClientName.Text, TBClientLocation.Text, TBCompany.SelectedValue.ToString(), TBPlace.Text, TBContactName.Text, TBContactPosition.Text, TBPartment.Text, TBPhone.Text, TBMark.Text,TBSalesMan.Text.Trim(), Convert.ToInt32(clientId)))
                {
                    if (repireBI.updateRepiredbyclientID(Convert.ToInt32(clientId), TBClientName.Text.Trim(), TBCompany.Text.Trim(), TBContactName.Text.Trim(), TBPlace.Text.Trim(),TBPhone.Text.Trim()))
                    {
                        MessageBox.Show("修改成功");
                    }
                    else
                    {
                        MessageBox.Show("修改报修表失败");
                    }
                }
                else
                    MessageBox.Show("修改失败");
            }
        }

        //添加汽车
        private void button1_Click(object sender, EventArgs e)
        {
            FormAddCar addCar = new FormAddCar(Convert.ToInt32(clientId), clientName);
            addCar.ShowDialog();
            if (addCar.DialogResult == DialogResult.Yes)
            {
                //初始化车的列表
                DataTable carsTable = clientBI.findCarsByClientId(clientId);
                this.carDataGridView.DataSource = carsTable;
            }
        }

        //添加维修记录
        private void button2_Click(object sender, EventArgs e)
        {
            String carId = carDataGridView.CurrentRow.Cells[7].Value.ToString();
            FormAddMainten addmainten = new FormAddMainten(TBClientName.Text, Convert.ToInt32(carId));
            addmainten.ShowDialog();
            if (addmainten.DialogResult == DialogResult.Yes)
            {
                
                DataTable maintenTable = clientBI.findCarMaintenRecordByCarId(carId);
                this.MaintenDataGridView.DataSource = maintenTable;
            }
        }

        //查看维修记录详情
        private void MaintenDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.MaintenDataGridView.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.RowIndex >= 0)
            {
                string id = MaintenDataGridView.CurrentRow.Cells[(MaintenDataGridView.CurrentRow.Cells.Count) - 1].Value.ToString();
                    MaintenInforForm mdForm = new MaintenInforForm(Convert.ToInt32(id));
                    mdForm.ShowDialog();
                    if (mdForm.DialogResult == DialogResult.Yes)
                    {
                        String carId = carDataGridView.CurrentRow.Cells[7].Value.ToString();
                        DataTable maintenTable = clientBI.findCarMaintenRecordByCarId(carId);
                        this.MaintenDataGridView.DataSource = maintenTable;
                    }
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (util.Ifpower(LoginInfo.CurrentUser.AccountName) == 1)
            {
                ExportExl exportExl = new ExportExl();
                exportExl.ExportToExcel(MaintenDataGridView, (DataTable)MaintenDataGridView.DataSource,1);
            }
            else
                MessageBox.Show("您没有导出excel的权限!");
        }

        private void FormClientDetail_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.DialogResult = DialogResult.Yes;
        }
    }
}
